package com.xuecheng.media.api;

import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamsDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author Mr.M
 * @version 1.0
 * @description 媒资文件管理接口
 * @date 2022/9/6 11:29
 */
@Tag(name = "媒资文件管理接口")
@RestController
public class MediaFilesController {


    @Autowired
    MediaFileService mediaFileService;


    @Operation(summary = "媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams , @RequestBody QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiels(companyId, pageParams, queryMediaParamsDto);

    }

    /**
     *
     * 前端发送数据类型
     *     url     localhost:8080/emp/1                                 @PathVariable 获取数据
     *     form    localhost:8080/emp/add?username='aa'&pwd='1234'      直接对象绑定 User
     *     json    localhost：8080/emp                                  @RequestBody
     *                {
     *                    username:'aa'
     *                    pwd:'1234'
     *                }
     *     multipart/form-data  提交的事文件                            @RequestPart("filedata") MultipartFile upload
     */

    /**
     *  文件上传前端:
     *  01   必须是 post 方法
     *  02   必须是 multipart/form-data 这个数据格式
     *
     *  后端开发:
     *  01 这个接口必须是 post
     *  02 接受 multipart/form-data 这个数据类型 要用  MultipartFile 类
     *  03  这个流MultipartFile 必须存储到图片服务器 返回url 存储到 MySQL中
     *
     * @param filedata
     * @return
     * @throws IOException
     */
    @Operation(summary = "上传文件")
    @PostMapping(
            value = "/upload/coursefile",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileResultDto upload(
            @RequestPart("filedata") MultipartFile filedata,
            @RequestParam(value = "folder",required=false) String folder,
            @RequestParam(value = "objectName",required=false) String objectName) throws IOException {

        Long companyId = 1232141425L;
        UploadFileParamsDto uploadFileParamsDto = new UploadFileParamsDto();
        //文件大小
        uploadFileParamsDto.setFileSize(filedata.getSize());
        //图片
        uploadFileParamsDto.setFileType("001001");
        //文件名称
        uploadFileParamsDto.setFilename(filedata.getOriginalFilename());//文件名称
        //文件大小
        long fileSize = filedata.getSize();
        uploadFileParamsDto.setFileSize(fileSize);
        //创建临时文件
        File tempFile = File.createTempFile("minio", "temp");
        //上传的文件拷贝到临时文件
        filedata.transferTo(tempFile);
        //文件路径
        String absolutePath = tempFile.getAbsolutePath();
        //上传文件
        UploadFileResultDto uploadFileResultDto = mediaFileService.uploadFile(
                companyId,
                uploadFileParamsDto,
                absolutePath);
        return uploadFileResultDto;
    }
}
